package me.azthink.myblog.listener;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import me.azthink.myblog.dao.NavDAO;
import me.azthink.myblog.dao.SiteDAO;
import me.azthink.myblog.dao.UserDAO;
import me.azthink.myblog.domain.Nav;
import me.azthink.myblog.domain.Site;
import me.azthink.myblog.domain.User;

/**
 * 启动监听器
 * @author 111
 *
 */
@WebListener
public class AppServletContextListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("应用销毁执行>>>>>>>>>>>>>>>>>>>>>");
	}

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("应用初始化执行>>>>>>>>>>>>>>>>>>>>>");
		
		// 将站点、博主、导航的数据，保存到应用域
		SiteDAO siteDAO = new SiteDAO();
		NavDAO navDAO = new NavDAO();
		UserDAO userDAO = new UserDAO();
		try {
			Site site = siteDAO.findById(1);
			List<Nav> navList = navDAO.findAll();
			User user = userDAO.findById(1);

			ServletContext application = sce.getServletContext();

			// 2.将站点信息保存到请求域
			System.out.println("站点名称：" + site.getTitle());
			System.out.println("站点博主：" + user.getNickname());
			System.out.println("导航数量：" + navList.size());
			application.setAttribute("site", site);
			application.setAttribute("navList", navList);
			application.setAttribute("user", user);

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
